{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}

module Result(Result(Success, Failure)) where

import Control.Monad.Error


data Result e a = Success a
                | Failure e


instance Monad (Result e) where
    return x   = Success x

    Success a >>= f   = f a
    Failure e >>= f   = Failure e


instance (Show e, Show a) => Show (Result e a) where
    show (Success a) = "OK[" ++ show a ++ "]"
    show (Failure e) = "FAIL[" ++ show e ++ "]"


instance MonadError e (Result e) where
    throwError = Failure

    catchError (Success x) handler = Success x
    catchError (Failure e) handler = handler e
